package routers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"order-man/handlers"
)

func Init() *gin.Engine {
	// 1.创建路由
	r := gin.Default()
	r.Use(Cors()) //默认跨域
	r.POST("/user/login", handlers.UserLogin)

	r.POST("/role/node/add", handlers.RoleNodeAdd) //角色权限添加

	r.GET("/user/role/list", handlers.UserRoleList) //用户角色列表

	r.GET("/role/node/list", handlers.RoleNodeList) //角色权限列表

	r.POST("/car/add", handlers.CarAdd) //车辆信息添加

	r.POST("/order/add", handlers.OrderAdd) //订单添加

	r.GET("/order/list", handlers.OrderList) //订单列表

	r.GET("/order/detail", handlers.OrderDetail) //订单详情

	//////////权限//////////

	r.GET("/user/list", handlers.UserList) //用户列表

	r.GET("/user/detail", handlers.UserDetail) //用户详情

	//////////仓库订单//////////

	r.POST("/repo/add", handlers.RepoAdd) //仓库添加

	r.POST("/repo/order/add", handlers.RepoOrderAdd) //仓库订单添加

	r.GET("/repo/order/list", handlers.RopeOrderList) //订单列表

	r.GET("/repo/order/detail", handlers.RopeOrderDetail) //订单详情

	return r
}
func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
